package com.sxy.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @author 宋鑫宇
 * @version 1.0
 */

public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception {
        //使用SqlSessionFactoryBean来创建SqlSessionFactory对象
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        //用setTypeAliasesPackage方法扫描包下的文件
        ssfb.setTypeAliasesPackage("com.sxy.pojo");
        //使用setDataSource用来获取数据库的连接
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        //扫描dao包
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.sxy.dao");
        return msc;
    }
}
